
angular.module('U9ImHd.controllers')

    .controller('HomeCtrl', ['$scope', '$ionicSlideBoxDelegate', '$ionicModal', '$ionicHistory', '$state', 'AppManager', 'User', 'Help', 'imanDialog', 'APPCONSTANTS', 
        function ($scope, $ionicSlideBoxDelegate, $ionicModal, $ionicHistory, $state, AppManager, User, Help, imanDialog, APPCONSTANTS) {
            $scope.data = {
                user: User.get(),
                appsArray: formatApps(AppManager.getApps())
            };

            $ionicModal.fromTemplateUrl('tpls/sidemenu.html', {
                scope: $scope,
                animation: 'slide-in-right'
            }).then(function(modal) {
                $scope.modal = modal;
            });
            
            $scope.showSideMenu = function () {
                $scope.modal.show();
            };
            $scope.onPagerClick = function (index) {
                $ionicSlideBoxDelegate.slide(index);
            };
            $scope.clickApp = function (app) {
                Help.loadApp(app.AppID).then(function () {
                    $state.go(app.AppID);
                }, function () {
                    imanDialog.toastCenter('加载应用失败，请确认是否已正确安装然后重试。');
                });
            };

            $scope.register = function () {
                $scope.modal.hide().then(function () {
                    $state.go('register');
                });
            };
            $scope.portSetting = function () {
                $scope.modal.hide().then(function () {
                    $state.go('portsetting');
                });
            };
            $scope.connectSetting = function () {
                $scope.modal.hide().then(function () {
                    $state.go('connectsetting');
                });
            };
            $scope.signout = function () {
                $scope.modal.hide().then(function () {
                    User.signout().then(function () {
                        $ionicHistory.nextViewOptions({
                            historyRoot: true,
                            disableAnimate: true,
                            expire: 300
                        });
                        $state.go('login');
                    });
                });
            };

            // $scope.$on('signin', function () {
            //     $scope.data.user = User.get();
            // });
            $scope.$on('$ionicView.afterEnter', function () {
                $scope.data.user = User.get();
            });
            $scope.$on('signout', function () {
                $scope.data.user = User.get();
            });
            $scope.$on('$destroy', function () {
                $scope.modal.remove();
            });

            function formatApps (apps) {
                var maxNum = APPCONSTANTS.appNumPerPage,
                    j = 0,
                    temp = [],
                    results = [];
                for (var i in apps) {
                    temp.push(apps[i]);
                    j++;
                    if (j === maxNum) {
                        j = 0;
                        results.push(temp);
                        temp = [];
                    }
                }
                if (apps.length % maxNum !== 0) {
                    results.push(temp);
                }
                return results;
            }
        }
    ]);